package com.itheima.day13.thread.test14;

// 吃货类
public class Foodie extends Thread {
    @Override
    public void run() {
        int count = 0;
        while (true) {
            // 上锁
            synchronized (Desk.obj) {
                // 判断是否汉堡是否吃完了
                if (Desk.count == 0)
                    break;
                // 判断桌子上面是否有汉堡包
                if (Desk.boo) {
                    // 有汉堡包
                    System.out.println("吃货正在吃汉堡包......");
                    try {
                        Thread.sleep(000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("吃完了，吃了 " + (++count) + "个汉堡包\n");
                    Desk.count--;
                    Desk.boo = false;
                    // 吃完汉堡后叫醒厨师继续做
                    Desk.obj.notify();
                } else {
                    // 无汉堡包
                    // 等待
                    try {
                        Desk.obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
